package com.renwei.yunlong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.renwei.yunlong.R;
import com.renwei.yunlong.bean.GuideCateInfo;
import com.renwei.yunlong.bean.GuideInfo;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

/**
 * Project :  yunna.
 * Package name: com.renwei.yunlong.adapter
 * Created by :  Benjamin.
 * Created time: 2018/1/31 18:37
 * Changed by :  Benjamin.
 * Changed time: 2018/1/31 18:37
 * Class description:
 */

public class GuideListAdapter extends BaseRecyclerViewAdapter<GuideCateInfo> {

    private static final int TYPE_TOP =1;
    private static final int TYPE_CONTENT =2;

    public GuideClickListener listener;
    public interface GuideClickListener {
        void onGuideClick(GuideInfo info);

    }
    public void setOnclickListener(GuideClickListener clickListener) {
        this.listener = clickListener;
    }

    public GuideListAdapter(Context context) {
        super(context);
    }

    @Override
    public int getItemViewType(int position) {
        if (TextUtils.isEmpty(mList.get(position).getCateName())){
            return TYPE_CONTENT;
        }else{
            return TYPE_TOP;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TOP){
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_guide_type, parent, false);
            return new ViewTopHolder(view);
        }else if (viewType == TYPE_CONTENT){
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_guide_info, parent, false);
            return new ViewContentHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        GuideCateInfo bean = getItem(position);
        if (getItemViewType(position)==TYPE_TOP){
            ViewTopHolder topHolder = (ViewTopHolder) holder;
            topHolder.onDataSet(bean,position);
        }else if (getItemViewType(position)==TYPE_CONTENT){
            ViewContentHolder contentHolder = (ViewContentHolder) holder;
            contentHolder.onDataSet(bean,position);
        }
    }

    class ViewTopHolder extends RecyclerView.ViewHolder {
        TextView type;

        public ViewTopHolder(View itemView) {
            super(itemView);
            type = itemView.findViewById(R.id.tv_guide_type);
        }

        public void onDataSet(GuideCateInfo bean,int position){
            type.setText(StringUtils.value(bean.getCateName()));
        }
    }

    class ViewContentHolder extends RecyclerView.ViewHolder {
        TextView content;
        ImageView ivIcon;

        public ViewContentHolder(View itemView) {
            super(itemView);
            content = itemView.findViewById(R.id.tv_guide_name);
            ivIcon = itemView.findViewById(R.id.iv_icon);
        }
        public void onDataSet(GuideCateInfo bean,int position){
            final GuideInfo guideInfo = bean.getGuideInfo();
            content.setText(StringUtils.value(guideInfo.getModelName()));
            if ("1".equals(guideInfo.getModelFlag())) {
                ivIcon.setImageResource(R.drawable.guide_ok);
            } else {
                ivIcon.setImageResource(R.drawable.guide_no);
            }
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onGuideClick(guideInfo);
                }
            });
        }

    }
}
